package gov.fnal.xpath2ldap;

import java.util.*;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import org.apache.commons.cli.CommandLine;

public class XQuerySearch {

       static StringBuilder expr_str = new StringBuilder()
       .append("xquery version \"1.0\"").append(";\n")
       .append("declare variable $vo as xs:string external;").append("\n")
       .append("string-join(").append("\n")
       .append("for $endpoint in //GlueSE/GlueSEControlProtocol[@GlueSEControlProtocolVersion='2.2.0']/@GlueSEControlProtocolEndpoint").append("\n")
       .append("  for $rootpath in $endpoint/../../GlueSA[@GlueSAFreeOnlineSize!=0]/GlueVOInfo[@GlueVOInfoAccessControlBaseRule=$vo]/@GlueVOInfoPath").append("\n")
       .append("    return concat(\"srm\", substring($endpoint, 6), \"?SFN=\", $rootpath,  \"/TESTFILE\", \"&#xA;\"),").append("\n")
       .append(" \"\")");
       static String expr = expr_str.toString();

    public static void main (String[] args) {

        if(args.length==0)  {
            args = new String[]{"--help"};
        }

        String responses = processXQueryExpression(args);
        if(responses.equals("0\n")) {
          System.err.println(responses);
        } else {
          System.out.println(responses);
        }
    }

    public static String processXQueryExpression(String[] args) {
        CommandLine cmdline = XQuerySearchOptions.processArguments(args);
        return processXQueryExpression(cmdline);
    }

    @SuppressWarnings("unchecked")
    public static String processXQueryExpression(CommandLine cmdline) {
        String responses = "";
        XQueryLDAPEvaluator xquerySearcher = new XQueryLDAPEvaluator(cmdline);
        List<String> argslist = cmdline.getArgList();
        if(cmdline.hasOption("test")) {
            String xquery_expression = expr;
            System.err.println(xquery_expression);
            responses = xquerySearcher.search(xquery_expression);
        } else {
            if(argslist.size()==0 && xquerySearcher.xqueryfile!=null) {
                responses = xquerySearcher.search(getContents(xquerySearcher.xqueryfile));
            } else {
                for (String xquery_expression : argslist) {
                    System.err.println(xquery_expression);
                    responses += xquerySearcher.search(xquery_expression);
                }
            }
        }

        return responses;
    }

  static public String getContents(String filename) {
    StringBuilder contents = new StringBuilder();

    try {
      BufferedReader input =  new BufferedReader(new FileReader(filename));
      try {
        String line;
        while (( line = input.readLine()) != null){
          contents.append(line);
          contents.append(System.getProperty("line.separator"));
        }
      }
      finally {
        input.close();
      }
    }
    catch (IOException ex){
      ex.printStackTrace();
    }

    return contents.toString();
  }
}